package com.maxmlr.music.admin.service.impl;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.google.common.collect.Lists;
import com.google.common.collect.Maps;
import com.maxmlr.music.admin.dao.MenuMapper;
import com.maxmlr.music.admin.dao.RoleMenuMapper;
import com.maxmlr.music.admin.entity.Menu;
import com.maxmlr.music.admin.entity.RoleMenu;
import com.maxmlr.music.admin.param.MenuParam;
import com.maxmlr.music.admin.param.TreeParam;
import com.maxmlr.music.admin.service.MenuService;
import com.maxmlr.music.admin.service.transfer.MenuTransfer;
import com.maxmlr.music.admin.util.BuildTreeParam;
import com.maxmlr.music.common.base.BaseServiceImpl;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;
import java.util.stream.Collectors;

/**
 * @author max
 * @version 1.0
 * @ClassName MenuServiceImpl
 * @Description  菜单管理  服务实现类
 * @Copyright Copyright (c) 2020: www.maxmlr.com.
 * @since 2020-06-14
 */
@Slf4j
@Service
public class MenuServiceImpl extends BaseServiceImpl<MenuMapper, Menu> implements MenuService {

    @Autowired
    private MenuTransfer menuTransfer;

    @Autowired
    private RoleMenuMapper roleMenuMapper;

    @Override
    public List<TreeParam<Menu>> listMenuTree(int i) {
        List<TreeParam<Menu>> trees = Optional.ofNullable(list())
                .orElse(Lists.newArrayList())
                .stream()
                .map(p -> menuTransfer.po2TreeParam(p))
                .collect(Collectors.toList());
        // 默认顶级菜单为０，根据数据库实际情况调整
        return BuildTreeParam.buildList(trees, "0");
    }

    @Override
    public MenuParam getMenu(Integer id) {
        MenuParam param = null;
        Menu menu = getById(id);
        if(Objects.nonNull(menu)){
            param=menuTransfer.po2MenuParam(menu);
            if (menu.getParentId() > 0) {
                Menu permentMenu = getById(menu.getParentId());
                if (Objects.nonNull(permentMenu)){
                    param.setParentName(permentMenu.getName());
                }
            }
        }
        return param;
    }

    @Override
    public TreeParam<Menu> getTreeParam() {
        List<TreeParam<Menu>> trees = Optional.ofNullable(list())
                .orElse(Lists.newArrayList())
                .stream()
                .map(p -> menuTransfer.po2TreeParam(p))
                .collect(Collectors.toList());
        return BuildTreeParam.build(trees);
    }

    @Override
    public TreeParam<Menu> getTreeParam(Long roleId) {
        List<Menu> menus = Optional.ofNullable(list()).orElse(Lists.newArrayList());

        List<Integer> roleMenuIds = Optional.ofNullable(roleMenuMapper.selectList(Wrappers.<RoleMenu>lambdaQuery().eq(RoleMenu::getRoleId, roleId)))
                .orElse(Lists.newArrayList())
                .stream()
                .map(RoleMenu::getMenuId)
                .collect(Collectors.toList());
        List<TreeParam<Menu>> treeParams = menus.stream()
                .map(p -> {
                    TreeParam<Menu> treeParam = menuTransfer.po2TreeParam(p);
                    Map<String, Object> state = Maps.newHashMap();
                    if (roleMenuIds.contains(p.getId())) {
                        state.put("selected", true);
                    } else {
                        state.put("selected", false);
                    }
                    treeParam.setState(state);
                    return treeParam;
                }).collect(Collectors.toList());
        return BuildTreeParam.build(treeParams);
    }
}
